<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Operation.php");						//--> Operation
include_once("Users.php");							//--> user
include_once("UserGroup.php");						//--> User Group
include_once("Validation.php");						//--> validation
require_once("controlHeader.php");					//--> system control header

$objOperate 	= new Operation($sessionImpl->getLanguage());		//--> Operation
$objUserGroup 	= new UserGroup($sessionImpl->getLanguage(), $uid);	//--> User Group
$objUser 		= new Users($uid);									//--> user
$objValidation 	= new Validation();									//--> Validation

$error_message 	= '';
$strGroupID		= '';
$strName 		= '';
$strPassword1	= '';
$strPassword2	= '';
$FirstName		= '';
$MiddleName 	= '';
$LastName 		= '';
$address 		= '';
$zipcode 		= '';
$telephone 		= '';
$mobile 		= '';
$fax 			= '';
$email 			= '';
$url 			= '';
$pwdQuestion	= '';
$pwdAnswer		= '';

$strStatus		= 'normal';

$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);
if(isset($_POST['Submit'])){
	$strGroupID 	= $_POST['groupid'];
	$strName 		= $_POST['name'];
	$strPassword1	= $_POST['password1'];
	$strPassword2 	= $_POST['password2'];
	$FirstName 		= $_POST['FirstName'];
	$MiddleName 	= $_POST['MiddleName'];
	$LastName 		= $_POST['LastName'];
	$address 		= $_POST['address'];
	$zipcode 		= $_POST['zipcode'];
	$telephone 		= $_POST['telephone'];
	$mobile 		= $_POST['mobile'];
	$fax 			= $_POST['fax'];
	$email 			= $_POST['email'];
	$url 			= $_POST['url'];
	$pwdQuestion 	= $_POST['pwdQuestion'];
	$pwdAnswer 		= $_POST['pwdAnswer'];
	
	$strStatus 		= $_POST['status'];
	
	if(empty($strGroupID))
		$error_message = 'The group ID should\'t be empty.';
	else if(empty($strName))
		$error_message = 'The user name should\'t be empty.';
	else if(empty($strPassword1))
		$error_message = 'The password should\'t be empty.';
	else if(empty($strPassword2))
		$error_message = 'The reset password should\'t be empty.';
	else if($strPassword1 != $strPassword2){
		$error_message = 'the two password is not different.';
	}else if($objUser->IsExistFromName($strName)){
		$error_message = 'the user name be existed.';
	}else if(!$objValidation->isEmail($email, "empty")){
		$error_message = 'The email should\'t be empty or invalid.';
	}else if(empty($pwdQuestion))
		$error_message = 'The password question should\'t be empty.';
	else if(empty($pwdAnswer)){
		$error_message = 'The password answer should\'t be empty.';
	}else{
		$isReturn = $objUser->add($arrOperate, $strGroupID, $strName, $strPassword1, $FirstName, $MiddleName, $LastName, $address, $zipcode, $telephone, $mobile, $fax, $email, $url, $pwdQuestion, md5($pwdAnswer), $strStatus);
		if($isReturn)
			$error_message = 'add user successfully.';
		else 
			$error_message = 'add user fail.';
	}
}

$arrUserGroup = $objUserGroup->arrGetGroupList($gid);

if(is_array($arrUserGroup) && count($arrUserGroup) > 0){
	if(empty($userGroupID))
		$userGroupID = key($arrUserGroup);
}

$arrStatus = array('normal' => 'Normal', 'stop' => 'Stop');

/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = CACHE_SYS_DIR;
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('backurl',$backurl);
$smarty->assign('error_message', $error_message);

$smarty->assign('arrUserGroup', $arrUserGroup);
$smarty->assign('groupidS', $strGroupID);
$smarty->assign('name', $strName);
$smarty->assign('password1', $strPassword1);
$smarty->assign('password2', $strPassword2);

$smarty->assign('arrStatus', $arrStatus);
$smarty->assign('StatusS', $strStatus);

$smarty->assign('FirstName', $FirstName);
$smarty->assign('MiddleName', $MiddleName);
$smarty->assign('LastName', $LastName);
$smarty->assign('address', $address);

$smarty->assign('zipcode', $zipcode);
$smarty->assign('telephone', $telephone);
$smarty->assign('mobile', $mobile);
$smarty->assign('fax', $fax);
$smarty->assign('email', $email);
$smarty->assign('url', $url);

$smarty->assign('pwdQuestion', $pwdQuestion);
$smarty->assign('pwdAnswer', $pwdAnswer);
	
$smarty->display('listUser_Add.htm');
?>
